/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package com.googlecode.chigs.domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.synyx.hades.domain.AbstractPersistable;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Entity
@NamedQueries({
		@NamedQuery(name = "Module.findByAll", query = "from Module o"),
		@NamedQuery(name = "Module.findByVersion", query = "select o from Module o join o.revisions r where r.version = ?"),
		@NamedQuery(name = "Module.findByRevisonDependencies", query = "select o from Module o join o.dependencies d where d = ?"),
		@NamedQuery(name = "Module.findByProductCount", query = "select count(distinct o) from Module o, in (o.revisions) r where r.version.product = ?")})
public class Module extends AbstractPersistable<Long> {
	private final static Logger log = LoggerFactory.getLogger(Module.class);	
	
	@Column(unique = true)
	private String name;
	
	@OneToMany(mappedBy = "module")
	private List<Revision> revisions;
	
	@ManyToMany(mappedBy = "dependencies")
	private List<Revision> dependencies; 

	public Module() {
		super();
		this.name = "";
		this.revisions = new ArrayList<Revision>();
		this.setDependencies(new ArrayList<Revision>());
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setRevisions(List<Revision> revisions) {
		this.revisions = revisions;
	}

	public List<Revision> getRevisions() {
		return revisions;
	}

	public void setDependencies(List<Revision> dependencies) {
		this.dependencies = dependencies;
	}

	public List<Revision> getDependencies() {
		return dependencies;
	}

	public Revision getLatestRevision() {
		if (revisions == null || revisions.isEmpty()) {
			log.debug("no revisions available:" + revisions == null ? "null" : "empty");
			return new Revision();
		}
		Collections.sort(revisions, new Comparator<Revision>() {
			@Override
			public int compare(Revision o1, Revision o2) {
				if (o1.getNr() > o2.getNr()) {
					return -1;
				} else if (o1.getNr() < o2.getNr()) {
					return 1;
				}
				return 0;
			}
		});
		return revisions.get(0);
	}
	
	@Override
	public String toString() {
		return String.format("Module [name=%s, getId()=%s]", name, getId());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((dependencies == null) ? 0 : dependencies.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((revisions == null) ? 0 : revisions.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (!(obj instanceof Module)) {
			return false;
		}
		Module other = (Module) obj;
		if (dependencies == null) {
			if (other.dependencies != null) {
				return false;
			}
		} else if (!dependencies.equals(other.dependencies)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (revisions == null) {
			if (other.revisions != null) {
				return false;
			}
		} else if (!revisions.equals(other.revisions)) {
			return false;
		}
		return true;
	}
}
